package xyz.heyaoshare.core.controller.system.role;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import xyz.heyaoshare.annotation.OperationLog;
import xyz.heyaoshare.common.entity.bo.Result;
import xyz.heyaoshare.common.entity.bo.ResultPage;
import xyz.heyaoshare.core.controller.system.role.vo.req.*;
import xyz.heyaoshare.core.controller.system.role.vo.resp.RoleDetailRespVO;
import xyz.heyaoshare.core.controller.system.role.vo.resp.RoleListRespVO;
import xyz.heyaoshare.core.controller.system.role.vo.resp.RolePageRespVO;
import xyz.heyaoshare.core.controller.system.role.vo.resp.RolePermissionRespVO;
import xyz.heyaoshare.core.service.system.RoleService;

import java.util.List;

import static xyz.heyaoshare.common.constants.VersionManager.V001;

/**
 * 角色相关
 * @author YueHe
 * @version 0.0.1
 * @since 2025/5/9 下午3:58
 */
@RestController
@RequestMapping(V001 + "/system/role")
@Tag(name = "角色相关")
public class RoleController {

    @Resource
    private RoleService roleService;

    @OperationLog
    @GetMapping("page")
    @Operation(summary = "角色分页")
    public Result<ResultPage<RolePageRespVO>> selectPage(@ParameterObject @Valid RolePageReqVO reqVO) {
        ResultPage<RolePageRespVO> items = roleService.selectPage(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("list")
    @Operation(summary = "角色列表")
    public Result<List<RoleListRespVO>> selectList(@ParameterObject @Valid RoleListReqVO reqVO) {
        List<RoleListRespVO> items = roleService.selectList(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping
    @Operation(summary = "角色详情")
    public Result<RoleDetailRespVO> selectDetail(@ParameterObject @Valid RoleDetailReqVO reqVO) {
        RoleDetailRespVO item = roleService.selectDetail(reqVO);
        return Result.ok(item);
    }

    @OperationLog
    @PostMapping("insert")
    @Operation(summary = "新增角色")
    public Result<Long> insert(@RequestBody @Valid RoleInsertReqVO reqVO) {
        Long roleId = roleService.insert(reqVO);
        return Result.ok(roleId);
    }

    @OperationLog
    @DeleteMapping("delete")
    @Operation(summary = "删除角色")
    public Result<Boolean> delete(@RequestBody @Valid RoleDeleteReqVO reqVO) {
        Boolean status = roleService.delete(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @PutMapping("update")
    @Operation(summary = "更新角色")
    public Result<Boolean> updateData(@RequestBody @Valid RoleUpdateReqVO reqVO) {
        Boolean status = roleService.updateData(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @PutMapping("update/status")
    @Operation(summary = "更新角色状态")
    public Result<Boolean> updateStatus(@RequestBody @Valid RoleUpdateStatusReqVO reqVO) {
        Boolean status = roleService.updateStatus(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @PutMapping("setPermissions")
    @Operation(summary = "设置角色权限")
    public Result<Boolean> setPermissions(@RequestBody @Valid RoleSetPermissionReqVO reqVO) {
        Boolean status = roleService.setPermissions(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @GetMapping("getPermissions")
    @Operation(summary = "获取角色权限")
    public Result<RolePermissionRespVO> getPermissions(@ParameterObject @Valid RolePermissionReqVO reqVO) {
        RolePermissionRespVO item = roleService.getPermissions(reqVO);
        return Result.ok(item);
    }

}